package com.youan.log.modules.audit.dataanalysis;

import com.youan.log.common.threadtask.ThreadTaskProgressor;
import com.youan.log.common.threadtask.ThreadTaskUtils;
import com.youan.log.modules.audit.dataimport.bean.TaskData;
import com.youan.log.modules.audit.entity.ImportRecord;
import com.youan.log.modules.audit.service.IImportRecordService;
import org.springblade.core.tool.utils.SpringUtil;

import static com.youan.log.modules.audit.dataimport.bean.ImportTaskKey.THREAD_TASK_STATUS_ATTACK;
import static com.youan.log.modules.audit.dataimport.bean.ImportTaskKey.THREAD_TASK_STATUS_ATTACK_TEXT;

public class AnalysisDataExtract {

    private String date;
    private ThreadTaskProgressor taskProgressor;
    private Long importRecordId;

    public AnalysisDataExtract(TaskData taskData, ThreadTaskProgressor taskProgressor, Long importRecordId) {
        this.date = taskData.getDate();
        this.taskProgressor = taskProgressor;
        this.importRecordId = importRecordId;
    }


    public void extract() {
        recordTaskStatus();
        this.taskProgressor.setProgressCommitSize(10);

        IP2Region ip2Region = new IP2Region();

        AttackAnalysisDataExtract attackExtract = new AttackAnalysisDataExtract(date, taskProgressor, ip2Region);
        attackExtract.extract();

        AttackedAnalysisDataExtract attackedExtract = new AttackedAnalysisDataExtract(date, taskProgressor, ip2Region);
        attackedExtract.extract();

        // 生成山东省攻击其他各省成功的次数
        SdAttackSuccessDataExtract sdAttackSuccessDataExtract = new SdAttackSuccessDataExtract(date, taskProgressor);
        sdAttackSuccessDataExtract.extract();

        // 数据库资产探测
        // 从源IP、目的IP以及端口数据中统计出承载着数据库的所有IP。不限制方向，累加存储，存入一张单独的表里。
        // 要求：IP去重，IP首次出现时间，IP归属国、省、市，运营商，后续将提供接口，定位到人。
        DbAssetSearchDataExtract dbAssetSearchDataExtract = new DbAssetSearchDataExtract(date, taskProgressor, ip2Region);
        dbAssetSearchDataExtract.extract();
        //  数据库被攻击事件查询
        //	找出山东境内各地（含未知地市）数据库被攻击的总次数，攻击成功的次数，攻击失败的次数，爆破成功的次数，爆破失败的次数。存到一张表里，
        //	每个地市一行，共16行，数据每天导入时随即更新。用的时候直接查询这张表。
        SdCityDbAttackedAnalysisDataExtract sdCityDbAttackedAnalysisDataExtract = new SdCityDbAttackedAnalysisDataExtract(date, taskProgressor);
        sdCityDbAttackedAnalysisDataExtract.extract();
        // 关于被攻击ip 对应每个攻击者的详细信息
        AttackedAttackerIpDetailDataExtract attackedAttackerIpDetailDataExtract = new AttackedAttackerIpDetailDataExtract(date, taskProgressor);
        attackedAttackerIpDetailDataExtract.extract();
        // 攻击时间查询
        // 去重统计出山东省内主动攻击其他地区的IP，记录首次发起攻击时间、最新攻击时间、该IP归属地区。
        SdAttackIpDetailDataExtract sdAttackIpDetailDetailDataExtract = new SdAttackIpDetailDataExtract(date, taskProgressor);
        sdAttackIpDetailDetailDataExtract.extract();

        ip2Region.close();
    }

    private void recordTaskStatus() {
        IImportRecordService recordService = SpringUtil.getBean(IImportRecordService.class);
        ImportRecord importRecord = recordService.getById(importRecordId);

        ImportRecord update = new ImportRecord();
        update.setId(importRecordId);
        update.setTaskStatus(THREAD_TASK_STATUS_ATTACK);
        recordService.updateById(update);

        ThreadTaskUtils.updateTaskStatus(importRecord.getTaskId(), THREAD_TASK_STATUS_ATTACK,
                THREAD_TASK_STATUS_ATTACK_TEXT);
    }


}
